package com.loop.ddz;

import java.util.*;

/**
 * 扑克牌类
 * 定义了获取牌组的花色和点数的方法，因为用的比较频繁，所以都定为静态方法
 */
public class Poker {
    //花色
    private String color;
    //点数
    private String num;
    //牌组的下标,此处定为静态是因为保证发牌后元素不重复
    public static ArrayList<Integer> indexList = new ArrayList<>();

    public static ArrayList<Integer> getIndexList() {
        return indexList;
    }

    public static void setIndexList(ArrayList<Integer> indexList) {
        Poker.indexList = indexList;
    }

    /**
     * 生成随机下标
     */
    /*static {
        //随机生成54个不重复的整数下标
        Random random = new Random();
        //这里用LinkedHashSet的原因是怕生成的随机数要是用HashSet可能会有排序的规律
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
        while (set.size()<54){
            set.add(random.nextInt(54));
        }
        //将set集合转换成list集合
        indexList = new ArrayList<>();
        for (int index : set) {
            indexList.add(index);
        }
    }*/

    /**
     * 使用Collections的shuffle方法打乱indexList中的元素位置，使其变成一个包含（1-54）随机元素的集合
     */
    static {
        for (int i = 0; i < 54; i++) {
            indexList.add(i);
        }
        Collections.shuffle(indexList);
    }

    public Poker() {
    }

    @Override
    public String toString() {
        return color+num+"   ";
    }

    public Poker(String color, String num) {
        this.color = color;
        this.num = num;

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Poker poker = (Poker) o;
        return Objects.equals(color, poker.color) &&
                Objects.equals(num, poker.num);
    }

    @Override
    public int hashCode() {
        return Objects.hash(color, num);
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getNum() {
        return num;
    }

    public final void setNum(String num) {
        this.num = num;
    }

    /**
     * 静态方法获取牌组的花色
     * @return 返回牌组花色的集合
     */
    public static List<String> getAllColor(){
        ArrayList<String> colorList = new ArrayList<>();
        colorList.add("♠");
        colorList.add("♣");
        colorList.add("♦");
        colorList.add("♥");
        return colorList;
    }

    /**
     * 静态方法获取牌组的点数
     * @return 返回牌组的点数集合
     */
    public static List<String> getAllNum(){
        ArrayList<String> numList = new ArrayList<>();
        numList.add("2");
        numList.add("A");
        numList.add("K");
        numList.add("Q");
        numList.add("J");
        for (int i = 10; i >=3 ; i--) {
            numList.add(String.valueOf(i));
        }
        return numList;
    }

    /**
     * 静态方法获取整个牌组
     * @return 返回整个牌组
     */
    public static List<Poker> getDek(){
        List<String> allNum = getAllNum();
        List<String> allColor = getAllColor();
        ArrayList<Poker> pokerList = new ArrayList<>();
        for (int i = 0; i < allNum.size(); i++) {
            for (int j = 0; j < allColor.size(); j++) {
                pokerList.add(new Poker(allColor.get(j),allNum.get(i)));
            }
        }
        pokerList.add(0,new Poker("","小王"));
        pokerList.add(0,new Poker("","大王"));
        return pokerList;
    }

    /**
     * 发牌方法
     */
    public static List<Poker> licensing(){
        //获取完整牌组
        List<Poker> pokerList = Poker.getDek();
        //定义随机数组获取随机数下标集合的Poker集合
        ArrayList<Poker> pokers = new ArrayList<>();
        int count = 0;
        ListIterator<Integer> iterator = indexList.listIterator();
        while (iterator.hasNext()){
            int index = iterator.next();
            pokers.add(pokerList.get(index));
            iterator.remove();
            count++;
            //每次发17张牌
            if (count > 16){
                break;
            }
        }
        //retainAll方法可以保留原数组的顺序
        pokerList.retainAll(pokers);
        return pokerList;
    }
}
